/*--------------------------------------------------------------------------
Merge inequality indicators with the individual level data
Last modification: 05/01/2021
Uses:	           perceptions_data.dta, merged_data.dta
Produces:          Regresions
--------------------------------------------------------------------------*/

global raw  "C:\Users\USERNAME\"
global code "${root}\code"
global data "${root}\data"
global res  "${root}\results"


#delimit;
use "${data}\perceptions_data.dta", replace;
merge m:1 pais ano using "${data}\merged_data.dta", nogen;
merge m:1 pais ano using "${data}\gini.dta"; drop if _merge == 2; drop _merge;

drop unfair; rename unfair_all unfair;
recode distribucion_justa (4=1) (3=2) (2=3) (1=4), gen(index);
tab maxedu, gen(educ);

gen     assets = 1 if cloaca == 1 & computadora  == 1 & lavarropas == 1 & telefono_fijo == 1 & auto == 1;
replace assets = 0 if cloaca == 0 | computadora  == 0 | lavarropas == 0 | telefono_fijo == 0 | auto == 0;

gen miss_assets   = (missing(cloaca) | missing(computadora) | missing(lavarropas) | missing(telefono_fijo) | missing(auto)); qui sum assets, d; replace assets = r(mean) if miss_assets == 1;
gen miss_catolico = (missing(catolico)); qui sum catolico, d; replace catolico = r(mean) if miss_catolico == 1;
gen miss_ideology = (missing(ideology)); qui sum ideology, d; replace ideology = r(mean) if miss_ideology == 1;

drop pol_boicot pol_piquete;
foreach var of varlist pol_* {; 
 recode `var' (1 2 = 0) (3   = 1), gen(d`var');
 recode `var' (1   = 0) (2 3 = 1), gen(q`var'); 
};

gen dpol_any = dpol_evadir + dpol_media + dpol_peticion + dpol_protestaileg; replace dpol_any = 1 if dpol_any > 0 & dpol_any != .;
gen qpol_any = qpol_evadir + qpol_media + qpol_peticion + qpol_protestaileg; replace qpol_any = 1 if qpol_any > 0 & qpol_any != .;

           levelsof ano , local(years) ; foreach yr of local years  {;          gen yr`yr' = (ano == `yr')  ; }; drop yr1997;
loc i = 0; levelsof pais, local(paises); foreach p  of local paises {; loc ++i; gen ct`i'  = (pais == "`p'"); }; drop ct1;

egen cluster = group(id_pais ano);
gen wt = pondera;

glo lel "_lel";
keep if ano <= 2015;


foreach dv in "unfair" "very_unfair" "index" {; glo dv `dv'; di "Depvar: `dv'";

#delimit;
eststo clear;
eststo: reghdfe ${dv} gini$lel                                                                                                          [pw=wt], absorb(ano id_pais) cluster(cluster);
eststo: reghdfe ${dv} gini$lel edad edad2 hombre                                                                                        [pw=wt], absorb(ano id_pais) cluster(cluster);
eststo: reghdfe ${dv} gini$lel edad edad2 hombre casado educ3 educ4                                                                     [pw=wt], absorb(ano id_pais) cluster(cluster);
eststo: reghdfe ${dv} gini$lel edad edad2 hombre casado educ3 educ4 pea desocupa                                                        [pw=wt], absorb(ano id_pais) cluster(cluster);
eststo: reghdfe ${dv} gini$lel edad edad2 hombre casado educ3 educ4 pea desocupa assets miss_asse                                       [pw=wt], absorb(ano id_pais) cluster(cluster);
eststo: reghdfe ${dv} gini$lel edad edad2 hombre casado educ3 educ4 pea desocupa assets miss_asse ideology miss_ideo catolico miss_cato [pw=wt], absorb(ano id_pais) cluster(cluster);

#delimit;
esttab using "${res}/${dv}_lpm.tex", drop(_cons miss*) 
coeflabels(
 gini "Gini"
 gini$lel "Gini"
 edad   "Age"
 edad2 "Age~squared"
 hombre "Male"
 casado "Married"
 educ3 "Finished~HS"
 educ4 "Finished~coll."
 pea "In~labor~force"
 desocupa "Unemployed"
 cloaca "Sewage "
 computadora "Computer"
 lavarropas "Washing~machine"
 telefono_fijo "Landline"
 auto "Has~access~to~a~car"
 assets "Assets~index"
 ideology "Conservative"
 catolico "Catholic"
)
replace nonotes nonumbers nomtitles se nolines nogaps obslast b(3) star(* 0.10 ** 0.05 *** 0.01) booktabs fragment stats(N 	`mean', fmt(%20.0fc %4.2f)) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");
};

foreach dv in "unfair" "very_unfair" {; glo dv `dv'; di "Depvar: `dv'";

eststo clear;
logit ${dv} gini$lel yr* ct*                                                                                                          [pw=wt], robust cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gini$lel yr* ct* edad edad2 hombre casado                                                                                 [pw=wt], robust cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gini$lel yr* ct* edad edad2 hombre casado educ3 educ4                                                                     [pw=wt], robust cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gini$lel yr* ct* edad edad2 hombre casado educ3 educ4 pea desocupa                                                        [pw=wt], robust cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gini$lel yr* ct* edad edad2 hombre casado educ3 educ4 pea desocupa assets miss_asse                                       [pw=wt], robust cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gini$lel yr* ct* edad edad2 hombre casado educ3 educ4 pea desocupa assets miss_asse ideology miss_ideo catolico miss_cato [pw=wt], robust cluster(cluster); eststo: margins, dydx(*) atmeans post;
esttab using "${res}/${dv}_logit.tex", drop(yr* ct* miss*)
coeflabels(
 gini "Gini"
 gini$lel "Gini"
 edad   "Age"
 edad2 "Age~squared"
 hombre "Male"
 casado "Married"
 educ2 "Primary~education"
 educ3 "Finished~HS"
 educ4 "Finished~coll."
 pea "In~labor~force"
 desocupa "Unemployed"
 cloaca "Sewage"
 computadora "Computer"
 lavarropas "Washing~machine"
 telefono_fijo "Landline"
 auto "Has~access~to~a~car"
 assets "Assets~index"
 ideology "Conservative"
 catolico "Catholic"
)
replace nonotes nonumbers nomtitles se nolines nogaps obslast b(3) star(* 0.10 ** 0.05 *** 0.01) booktabs fragment stats(N, fmt(%20.0fc)) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");


* Different inequality indicators instead of Gini - 5 different indicators one per column;
#delimit;
eststo clear;
logit ${dv} gini_nozero  edad edad2 hombre casado educ3 educ4 pea desocupa assets ideology catolico miss* yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} atk_1        edad edad2 hombre casado educ3 educ4 pea desocupa assets ideology catolico miss* yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} theil        edad edad2 hombre casado educ3 educ4 pea desocupa assets ideology catolico miss* yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gen_entrophy edad edad2 hombre casado educ3 educ4 pea desocupa assets ideology catolico miss* yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post;
logit ${dv} gini_abs     edad edad2 hombre casado educ3 educ4 pea desocupa assets ideology catolico miss* yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post;
esttab using "${res}/${dv}_ineq_logit.tex", drop(yr* ct* miss*) 
coeflabels(
 gini_nozero "Gini~OPAREN no~zero~income CPAREN"
 atk_1 "Atkinson,~AOPAREN1CPAREN"
 theil "Theil~index"
 gen_entrophy "Generalized~entropy"
 gini_abs "Absolute~Gini"
 edad   "Age"
 edad2 "Age~squared"
 hombre "Male"
 casado "Married"
 educ2 "Primary~education"
 educ3 "Finished~HS"
 educ4 "Finished~coll."
 pea "In~labor~force"
 desocupa "Unemployed"
 cloaca "Sewage "
 computadora "Computer"
 lavarropas "Washing~machine"
 telefono_fijo "Landline"
 auto "Has~access~to~a~car"
 assets "Assets~index"
 ideology "Conservative"
 catolico "Catholic"
)
replace nonotes nonumbers nomtitles se nolines nogaps obslast b(3) star(* 0.10 ** 0.05 *** 0.01) booktabs fragment stats(N, fmt(%20.0fc)) order(gini_nozero atk_1 theil gen_entrophy gini_abs) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");

};


#delimit;
foreach dv in "unfair" "very_unfair" "index" {; glo dv `dv'; di "Depvar: `dv'";

* Activism;
loc controls "edad edad2 hombre casado educ2 educ3 educ4 pea desocupa assets ideology catolico miss*";
if "${dv}" == "unfair"      loc lab "Unfair"     ;
if "${dv}" == "very_unfair" loc lab "Very~unfair";
if "${dv}" == "index"	    loc lab "Raw~unfairness~index";

eststo clear;
logit dpol_redessoc     ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_redessoc     if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_media        ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_media        if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_peticion     ${dv} gini$lel `controls' yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_peticion     if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_protestaleg  ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_protestaleg  if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_protestaileg ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_protestaileg if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_evadir       ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_evadir       if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_disturbios   ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_disturbios   if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_ocupa        ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_ocupa        if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit dpol_any          ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum dpol_any          if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';


esttab using "${res}/${dv}_unrest_past.tex", keep(${dv} gini*) coeflabels(${dv} "`lab'" gini_lel "Gini") replace nonotes nonumbers nomtitles se nolines nogaps noobs b(3) star(* 0.10 ** 0.05 *** 0.01) booktabs fragment stats(mean, fmt(%4.3f) layout("\multicolumn{1}{c}{@}") labels("Mean~Dep.~Var.")) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");

esttab using "${res}/${dv}_unrest_past_N.tex", keep(${dv}) drop(${dv}) replace booktabs nonotes nonumbers nomtitles se b(3) noobs nolines nogaps nostar fragment stats(N, fmt(%20.0fc) layout("\multicolumn{1}{c}{@}")) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");


eststo clear;
logit qpol_redessoc     ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_redessoc     if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_media        ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_media        if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_peticion     ${dv} gini$lel `controls' yr* ct* [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_peticion     if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_protestaleg  ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_protestaleg  if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_protestaileg ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_protestaileg if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_evadir       ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_evadir       if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_disturbios   ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_disturbios   if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_ocupa        ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_ocupa        if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';
logit qpol_any          ${dv} gini$lel `controls'         [pw=wt], robust noomitted cluster(cluster); eststo: margins, dydx(*) atmeans post; qui sum qpol_any          if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean';

esttab using "${res}/${dv}_unrest_would.tex", keep(${dv} gini*) coeflabels(${dv} "`lab'" gini_lel "Gini") replace nonotes nonumbers nomtitles se nolines nogaps noobs b(3) star(* 0.10 ** 0.05 *** 0.01) booktabs fragment stats(mean, fmt(%4.3f) layout("\multicolumn{1}{c}{@}") labels("Mean~Dep.~Var.")) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");

esttab using "${res}/${dv}_unrest_would_N.tex", keep(${dv}) drop(${dv}) replace booktabs nonotes nonumbers nomtitles se b(3) noobs nolines nogaps nostar fragment stats(N, fmt(%20.0fc) layout("\multicolumn{1}{c}{@}")) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");

eststo clear;
eststo: reghdfe protesta_trabajo    ${dv} gini$lel `controls' [pw=wt], absorb(ano id_pais) cluster(cluster); qui sum protesta_trabajo    if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean'; 
eststo: reghdfe protesta_democracia ${dv} gini$lel `controls' [pw=wt], absorb(ano id_pais) cluster(cluster); qui sum protesta_democracia if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean'; 
eststo: reghdfe protesta_educacion  ${dv} gini$lel `controls' [pw=wt], absorb(ano id_pais) cluster(cluster); qui sum protesta_educacion  if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean'; 
eststo: reghdfe protesta_naturaleza ${dv} gini$lel `controls' [pw=wt], absorb(ano id_pais) cluster(cluster); qui sum protesta_naturaleza if e(sample), mean; local mean : di %4.3f r(mean); estadd local mean `mean'; 
esttab using "${res}/${dv}_unrest_will.tex", keep(${dv} gini*) coeflabels(${dv} "`lab'" gini_lel "Gini") replace nonotes nonumbers nomtitles se nolines nogaps obslast b(3) star(* 0.10 ** 0.05 *** 0.01) booktabs fragment stats(mean N, fmt(%4.3f %20.0fc) layout("\multicolumn{1}{c}{@}") labels("Mean~Dep.~Var." "N")) substitute(\_ _ " " "" "&" "&&" "~" " " "-" "$-$" "\\" "&\\" "\multicolumn{1}{c}{" "" "}&" "&" "\sym{***&" "&\sym{***}" "\sym{**&" "&\sym{**}" "\sym{*&" "&\sym{*}" ")&" "&)" "\&)" "\)&" "OPAREN" "(" "CPAREN" ")" "(.&)" "&");

};



exit
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
